#include "msgQueue.h"
#include "bank.h"
#include "tools.h"

//通信消息
ClientToServe ctos;
ServeToClient stoc;

//消息队列ID
int stocMsgId = 0;
int ctosMsgId = 0;

void openAccount(void)
{
	//获取银行卡号
	sprintf(ctos.acc.bank,"%d",createCardNo(BANK_PATH));

	//设置初始密码
	sprintf(ctos.acc.password,"123456");

	//设置锁定状态
	ctos.acc.isLock = 0;

	char path[PATH_LEN] = {};
	sprintf(path,"%s%s",ACC_PATH,ctos.acc.bank);

	int fd = open(path,O_WRONLY|O_CREAT|O_EXCL,0644);
	if(0 > fd)
	{
		error("open");
		sprintf(stoc.info,"N:服务器正在升级，开户失败!");
		return;
	}

	write(fd,&ctos.acc,sizeof(ctos.acc));
	sprintf(stoc.info,"Y:开户成功，卡号:%s 密码:%s",ctos.acc.bank,ctos.acc.password);
	close(fd);
}

int main()
{
	//获取两个消息队列
	stocMsgId = getMsg(STOC_KEY);
	ctosMsgId = getMsg(CTOS_KEY);

	while(true)
	{
		ctos.type = M_OPEN;
		receiveMsg(ctosMsgId,&ctos,sizeof(ctos));

		openAccount();

		stoc.type = ctos.pid;
		sendMsg(stocMsgId,&stoc,sizeof(stoc));
	}
}
